/**
 * Copyright (c) 2008, Keisuke Inoue, Syracuse Unviersity
 * All rights reserved.
 *
 */
package edu.syr.iis.web.model;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.HibernateException;
import org.hibernate.Session;

import edu.syr.iis.web.util.HibernateUtil;

/**
 * @author kinoue
 *
 */
public class DialogueActManager
{
	/**
	 * Saves a DialogueAct object.
	 */
	public DialogueAct saveDialogueAct(DialogueAct dialogueAct)
	{
		DialogueAct savedDa = null;
		Session session = HibernateUtil.getSessionFactory()
		.getCurrentSession();
		session.beginTransaction();
		try
		{
			savedDa = (DialogueAct) session.merge(dialogueAct);
			System.out.println("Saved a DialogueAct with an id: " + savedDa.dialogueActId);
		}
		catch (HibernateException e)
		{
			session.getTransaction().rollback();
			throw e;
		}
		return savedDa;
	}


/**
 * Update a DialogueAct object.
 */
public void updateDialogueAct(DialogueAct dialogueAct)
{
	System.out.println("Entering saveDialogueAct...");

	Session session = HibernateUtil.getSessionFactory()
	.getCurrentSession();
	session.beginTransaction();
	try
	{            
		session.saveOrUpdate(dialogueAct);
		System.out.println("Updated a DialogueAct with an id: " + dialogueAct.getDialogueActId());
		session.getTransaction().commit();
	}
	catch (HibernateException e)
	{
		session.getTransaction().rollback();
		throw e;
	}

}


public DialogueAct getDialogueAct(int dialogueActId)
{
	DialogueAct dialogueAct = null;
	Session session = HibernateUtil.getSessionFactory().getCurrentSession();
	session.beginTransaction();

	System.out.println("---------- Getting dialogueAct of dialogueActId: " + dialogueActId + "----------------");
	try
	{
		dialogueAct = (DialogueAct) session.createQuery(
				"FROM DialogueAct " +
		" WHERE dialogueActId = ?").setInteger(0, dialogueActId).uniqueResult();
		session.getTransaction().commit();
	}
	catch (HibernateException e)
	{
		session.getTransaction().rollback();
		throw e;
	}
	return dialogueAct;
}

@SuppressWarnings("unchecked")

public List<DialogueAct> getDialogueActs()
{
	List<DialogueAct> list = null;
	Session session = HibernateUtil.getSessionFactory().getCurrentSession();
	session.beginTransaction();

	try
	{
		list = session.createQuery("FROM DialogueAct ORDER BY dialogueActId ").list();
		session.getTransaction().commit();
	}
	catch (HibernateException e)
	{
		session.getTransaction().rollback();
		throw e;
	}
	return list;
}

public List<DialogueAct> getDialogueActsNonLazy()
{
	List<DialogueAct> list = null;
	Session session = HibernateUtil.getSessionFactory().getCurrentSession();
	session.beginTransaction();

	try
	{					
		Criteria criteria = session.createCriteria(DialogueAct.class);
		criteria.setFetchMode("function", FetchMode.EAGER);
		criteria.setFetchMode("domain", FetchMode.EAGER);
		list = criteria.list();
		
		session.getTransaction().commit();
	}
	catch (HibernateException e)
	{
		session.getTransaction().rollback();
		throw e;
	}
	return list;
}


public void deleteDialogueAct(DialogueAct da)
{
	Session session = 
		HibernateUtil.getSessionFactory().getCurrentSession();
	session.beginTransaction();	
	try
	{
		if (da != null) { 
			session.delete(da);
		}		
		session.getTransaction().commit();
	}
	catch (HibernateException e)
	{
		session.getTransaction().rollback();
		throw e;
	}	
}


/**
 * @param daId
 */
public void deleteDialogueAct(int daId)
{
	Session session = 
		HibernateUtil.getSessionFactory().getCurrentSession();
	session.beginTransaction();	
	try
	{
		Object toDelete = session.get(DialogueAct.class,new Integer(daId));

		if (toDelete != null) { 
			session.delete(toDelete);
		}		
		session.getTransaction().commit();
	}
	catch (HibernateException e)
	{
		session.getTransaction().rollback();
		throw e;
	}	
}


}
